<?php

namespace app\admin\Controller;


use app\admin\model\ClassroomModel;
use app\admin\Model\MajorModel;
use app\admin\Model\PlistModel;
use app\admin\Model\PointModel;
use app\admin\Model\ReasonModel;
use app\admin\Model\School as SchoolModel;
use app\admin\model\StudentModel;
use app\admin\Model\UserModel;

use cmf\controller\AdminBaseController;

use think\Db;
use think\db\Where;
use think\Session;
use think\Validate;


class PlistController extends AdminBaseController
{
    public function index () {
        extract(input());
        $hasWhere = [];
        $where = [] ;
        if (isset($name) && !empty($name)){
            $hasWhere[] = ['name','like','%'.$name.'%'];
        }
        if(isset($rid) && !empty($rid)){
            $where[] = ['rid','=',$rid];
        }
        $data = PlistModel::hasWhere('student',$hasWhere)->with(['point','reason','student','user','admin'])->where($where)->order('id Desc')->paginate(3);
        //halt($data);
        foreach($data as &$v){
            $v['Create_time'] = date('Y-m-d H:i:s', $v['Create_time']);
            $v['Examine_time'] = date('Y-m-d H:i:s', $v['Examine_time']);
        }

        $page = $data->render();
        return $this->fetch('',[
            'data' => $data,
            'page' => $page,
        ]);
    }
    public function add() {
        $school = SchoolModel::all();
        $reason = ReasonModel::all();
        $Point =  PointModel::all();
        $plist = PlistModel::all();

        $user = UserModel::all();

        //halt($user);
        $this->assign('user',$user);


        $this->assign('school',$school);
        $this->assign('plist',$plist);
        $this->assign('reason',$reason);
        $this->assign('point',$Point);

        return $this->fetch();
    }
    public function check(){
        $id = input('id');
        $type = input('type');
        if ($type == 1) {
            $status = 1;
        } else {
            $status = 2;
        }

        $edit = PlistModel::where('id', $id)->update([
            'status' => $status,
            'handle_time' => time()
        ]);
        if($edit){
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }

}
    public function Get_major()
    {
        $schoolId = input('school_id');
        $major = MajorModel::where('school_id', $schoolId)
            ->select();
        $options = ["<option value=''>请选择专业</option>"];
        foreach ($major as $v) {
            $options[] = "<option value='" . $v['id'] . "'>" . $v['major_name'] . "</option>";
        }
        return $options;
    }

    public function Get_class()
    {
        $majorID = input('major_id');
        $major = ClassroomModel::where('major_id', $majorID)
            ->select();
        $options = ["<option value=''>请选择班级</option>"];
        foreach ($major as $v) {
            $options[] = "<option value='" . $v['id'] . "'>" . $v['class_name'] . "</option>";
        }
        return $options;
    }
    public function Get_student()
    {
        $studentID = input('class_id');
        $student = StudentModel::where('class_id', $studentID)
            ->select();
        $options = ["<option value=''>请选择学生</option>"];
        foreach ($student as $v) {
            $options[] = "<option value='" . $v['id'] . "'>" . $v['name'] . "</option>";
        }
        return $options;
    }
    public function score(){
        $id = input('sid');
        $data = PlistModel::with(['student','point'])
            ->where('sid',$id)->paginate();
        //halt($data);
        $page = $data->render();
        return $this->fetch('',[
            'data' => $data,
            'page' => $page
        ]);
    }
    public function score2(){
        $id = input('sid');
        $data = Db::name('Plist')->alias('Pli')
            ->join('student stu', 'stu.id=Pli.sid')
            ->join('point po','po.id = Pli.pid')
            ->join('reason rea','rea.id=Pli.rid')
            ->field('Pli.id,stu.name')
            ->where('sid',$id)
            ->paginate(3);
        halt($data);
            $page = $data->render();
            return $this->fetch('score',[
                'data' => $data,
                'page' => $page
            ]);
    }
    public function addPost() {
        $data = input();
        $rule = [
            'pid' => 'require',
            'sid' => 'require',
            'rid' => 'require',
            'desc' => 'require',
        ];
        $msg = [
            'pid.require' => '分值不能为空',
            'sid.require' => '学生不能为空',
            'rid.require' => '原因不能为空',
            'desc.require' => '备注不能为空',
        ];
        $validate = new Validate($rule, $msg);
        $result = $validate->check($data);
        if (true !== $result) {
            $this->error($validate->getError());
        }
        $data['user_id'] = cmf_get_current_admin_id();
        $data['Create_time'] = time();
        $add = PlistModel::create($data);
        if ($add){
            return json(['code' => 1 ,'msg' => '添加成功']);
        }else{
            return json (['code' => 2 ,'msg' => '添加失败']);
        }
    }
    public function edit()
    {
        $id = input('id');
        $user = UserModel::all();
        //halt($user);
        $this->assign('user',$user);
        // 查询要修改的数据
        $Plist =PlistModel::where('id', $id)
            ->find();
        $this->assign('plist', $Plist);

        $reason = ReasonModel::all();
        $this->assign('reason',$reason);

        $point = PointModel::all();
        $this->assign('point',$point);

        $all = PlistModel::all();
        $this->assign('all',$all);

        $student = StudentModel::all();
        $this->assign('student',$student);

        $student =StudentModel::find($id);
        $school =SchoolModel::all();

//        $classId =StudentModel::where('id', $score['student_id'])->value('class_id');
        // 找到该学生所在专业 ID
        $majorId =ClassroomModel::where('id', $student['class_id'])->value('major_id');
        // 找到该学生所在学校的 ID
        $schoolId =MajorModel::where('id', $majorId)->value('school_id');
        // 查询该学生所在学校下的专业
        $major =MajorModel::where('school_id', $schoolId)->select();
        // 查询该学生所在专业下的班级
        $class =ClassroomModel::where('major_id', $majorId)->select();
        // 查询该班级下的学生
//        $students =StudentModel::where('class_id', $classId)->select();
        $this->assign('id', $id);
        return $this->fetch('', [
            'data' => $student,
//            'student' => $students,
            'school' => $school,
            'majorId' => $majorId,
            'schoolId' => $schoolId,
            'major' =>$major,
            'class' => $class
        ]);
    }
    public function editPost(){
        $data = input();

        $edit = PlistModel::where('id',$data['id'])->update($data);
        if ($edit) {
            return json(['code' => 1 , 'msg' => '修改成功']);
        }else{
            return json(['code' => 2 , 'msg' => '修改失败']);
        }
    }
    public function delete () {
        $id = input('id');
        $del = PlistModel::destroy($id);
        if($del){
            $this->success('删除成功','Plist/index');
        }else{
            $this->error('删除失败','Plist/index');
        }
    }
    public function delAll() {
        $ids = input('ids/a');
        $del = PlistModel::destroy($ids);
        if($del){
            $this->success('删除成功','Plist/index');
        }else{
            $this->error('删除失败','Plist/index');
        }
    }
}